package com.example.webview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

/**
 * 原生开发，h5做过吗，怎么和js交互
 */
public class MainActivity extends AppCompatActivity {
    private WebView webView;
    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.web_view);

        //加载html
//        webView.loadUrl("");
        webView.loadUrl("file:///android_asset/index.html");
        webView.setWebViewClient(new WebViewClient()); //保证网址在app中加载，而不是打开浏览器
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                Log.d("TAG", "onProgressChanged: "+newProgress);
            }
        });

        //设置允许和js交互
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(this,"ivideo");

    }
    @JavascriptInterface
    public void toast(){
        Toast.makeText(this,"安卓的吐司",Toast.LENGTH_LONG).show();
    }

    public void changeText(View view) {
        webView.loadUrl("JavaScript:changeText()"); //直接调用js
    }

    public void changeImage(View view) {//直接调用js
        webView.loadUrl("JavaScript:changeImage('https://profile-avatar.csdnimg.cn/b6efd2e74a594207a2732ffa918e5d19_qq_34178710.jpg!1')");
    }
}